$(function(){
	/*获取文章信息*/
	var geta = ym.getQueryStringByName('a');
	if(geta == 0){
		$('#goBackIcon').attr('href','javascript:history.go(-1)');
	}else{
		$('#goBackIcon').attr('href','personal.html');
	}
	
	var curData=JSON.parse(window.sessionStorage.getItem("curData"));
	console.log(curData)
	var personHeadHTML="";
	var datas=[];
	function getDoctorForm(){
		personHeadHTML +="<div class='person-tab-head'>"
						personHeadHTML +="<div class='doctor-head-image am-margin-top-sm'><img src='"+curData.headimgurl+"'></div>"
						personHeadHTML +="<div class='person-name am-margin-left-sm w80'>"
							personHeadHTML +="<h4 class='doctor-name-text color4e font16'>"+curData.doctorRealName+"</h4>"
							personHeadHTML +="<div class='doctor-desc'>"
								personHeadHTML +="<p class='margin02'>地址:"+curData.stationName+"</p>"
								if(curData.workTime == null){
									personHeadHTML +="<p class='margin02'>在线时间：暂未定</p>"
								}else{
									personHeadHTML +="<p class='margin02'>在线时间："+curData.workTime+" </p>"
								}
								personHeadHTML +="<p>介绍:"+curData.des+"</p>"
							personHeadHTML +="</div>"
						personHeadHTML +="</div>"
						personHeadHTML +="<div class='contact-doctor line am-text-right w25'>"
							personHeadHTML +="<button>"+curData.douctorConsultTypeName+"</button>"
						personHeadHTML +="</div>"
						personHeadHTML +="</div>";
	$('.doctorForm').html(personHeadHTML);
	}
	getDoctorForm();
	/*获取该问题问题列表*/
	function getProblemList(){
		var listHTML="";
		var zanHTML="";
		var imgListHTML="";
		ym.ajax({
				url: "/Answer/queryYmAnswerList",
				param:{
					type:'2',
					typeId:curData.doctorId,
					praiseType:4,
				},
				success: function (data) {
				  var newData=data;
				   if(newData.obj){
				   	datas=newData.obj;
				   		for (var i = 0; i < newData.obj.length; i++) {
				   			/*显示是否被赞*/
				   			zanHTML="";
				   			if(newData.obj[i].praiseState===1){
				   				zanHTML+="<span><img src='./../img/icon-active-dianzan.png'></span>";
				   			}else{
				   				zanHTML+="<span><img src='./../img/icon-dianzan.png'></span>";
				   			}
				   			if(newData.obj[i].ymAttachments.length!==0&&newData.obj[i].ymAttachments!==null){
				   				imgListHTML="";
								for (var j = 0; j <newData.obj[i].ymAttachments.length; j++) {
									imgListHTML+="<li style='width:30%;overflow:hidden;display:inline-block;margin-right:0.5rem'><img src="+newData.obj[i].ymAttachments[j].attachmentUrl+" style='width:100%;max-height:100px'></li>";
								}
								
							}else{
								imgListHTML="";
							}
				   			listHTML+="<div class='problem-tab content-tab am-padding-bottom-0' curType='"+newData.obj[i].type+"' curId="+newData.obj[i].answerId+" curAnserId='"+newData.obj[i].answerUserId+"' curTypeId='"+newData.obj[i].typeId+"'>"+
										  "<div class='person-tab-head'>"+
										  		"<div class='head-image'><img src='"+newData.obj[i].headimgurl+"' ></div>"+
												"<div class='person-name'>"+
													"<h4 class='person-name-text '>"+newData.obj[i].userName+"</h4>"+
													"<div class='add-problem-time w80'><p class='font12 color93'>"+newData.obj[i].createTimeString+"</p></div>"+
												"</div>"+
												 "<div class='problem-pinluncount am-text-right w30'><p><span><img src='./../img/icon-pinglun.png' ></span><sup> "+newData.obj[i].answersCount+" </sup></p></div>"+
										  "</div>"+
										  "<div class='problem-tab-desc'>"+
										    "<p class='color4e'>"+newData.obj[i].answerDes+"</p>"+
										    "<ul class='am-margin-top-xs'>"+imgListHTML+"</ul>"+
										  "</div>"+
										"</div>";
				   		} 
				   	$('#lists').html(listHTML)
				   }
				}
			});
	}
	getProblemList();
	/*回复*/
	function reply(dataJson){
		ym.ajax({
	        url: "/communication/insertConsult",
	        param:dataJson,
	        success: function (data) {
	        }
	    });
	}
	$('body').on('click','.plgr',function(){
    	var id = $(this).parent().siblings('p').find('.usid').text();
    	var name = $(this).parent().siblings('p').find('.usna').text();
    	var typeid = $(this).parent().siblings('p').find('.typeid').text();
    	$('.release').children('input').focus();
    	$('.release').children('input').attr('placeholder','回复:'+name);
    	$('body').on('click','.fabu',function(){
    		var answerDes = $('.huifucon').val();
	    	var dataJson={
	    		url:urls,
	            answerDes:answerDes,
	            typeId:exchangeId,
	            answerUserId:1,
	            parentId:id
	    	}	
    		reply(dataJson);
    	})
    })
    $('body').on('click','.fabu',function(){
    		var answerDes = $('.huifucon').val();
    		var dataJson={
    			url:urls,
	            answerDes:answerDes,
	            typeId:exchangeId,
	            answerUserId:1
    		}
			reply(dataJson)
	})
	/*查看提问详情*/
	$('body').on('click','#lists .problem-tab',function(){
			var curId=$(this).attr('curId');
			var dataJson={};
			for (var i = 0; i < datas.length; i++) {
						if(datas[i].answerId===parseInt(curId)){
							ym.ajax({
								url: "/Answer/queryYmAnswerListDetail",
								param:{
									typeId:$(this).attr('curTypeId'),
									answerId:datas[i].answerId,
									praiseType:4,
									parentAnswerId:datas[i].answerId
								},
								success: function (data) {
									sessionStorage.setItem('articleXq',JSON.stringify(data.obj));		
									window.location.href="problemXq.html";
								}
							});
						}
			}
		})
	/*向医生提问*/
	$('#mendb').click(function(){
		if(curData.quota==0){
			$('#mendb button').attr('disabled','disabled');
			alert('该医生的提问次数已用完！');
		}else{
			window.location.href='issueToDoctor.html';
			sessionStorage.setItem('doctorId',curData.doctorId);
		}
		
	})
	/*如果医生的回答问题总数等于0时，不能再提问*/
	
})